package com.whmnx.chitchat.ui.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.CommonRecyclerAdapter;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.qcloud.tim.uikit.component.TitleBarLayout;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.contact.ContactLayout;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.adapter.DepAdapter;
import com.whmnx.chitchat.adapter.UserAdpter;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.DepartmentDean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.ui.book.ChuangFriendsActivity;
import com.whmnx.chitchat.ui.book.DepartmentActivity;
import com.whmnx.chitchat.ui.book.DepartmentActivity2;
import com.whmnx.chitchat.ui.book.GroupActivity;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/8/14 0014 17:25
 * version: 1.0
 * @author Administrator
 */
public class MiContactsActivity extends BaseActivity {

    @BindView(R.id.rl_dep)
    RecyclerView rlDep;
    @BindView(R.id.rl_user)
    RecyclerView rlUser;
    @BindView(R.id.contact_layout)
    ContactLayout contactLayout;
    @BindView(R.id.rl_head)
    RelativeLayout rl_head;

    @BindView(R.id.tv_people)
    TextView tvPeople;
    @BindView(R.id.message_search)
    EditText query;

    DepAdapter depAdapter;
    UserAdpter userAdpter;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.boss_fragment);
        setTitle("发起密聊");
        rl_head.setVisibility(View.GONE);
    }

    @Override
    protected void loadData() {
        boolean flag = getIntent().getBooleanExtra("isTopChat",false);
        rlDep.setLayoutManager(new LinearLayoutManager(this));
        rlDep.setItemAnimator(new DefaultItemAnimator());
        depAdapter = new DepAdapter(this);
        rlDep.setAdapter(depAdapter);
        rlUser.setLayoutManager(new LinearLayoutManager(this));
        rlUser.setItemAnimator(new DefaultItemAnimator());
         userAdpter = new UserAdpter(this);
        rlUser.setAdapter(userAdpter);

        TitleBarLayout titleBarLayout = contactLayout.findViewById(R.id.contact_titlebar);
        titleBarLayout.setVisibility(View.GONE);
        //  ibRight.setOnClickListener(this);
        //  ivUser.setOnClickListener(this);
        Map<String, Object> params = new HashMap<>();
        params.put("OrganizeId", "");
        params.put("DepartmentID", "");
        HttpClient.get(this, Constants.GETDEPARTMENTLIST, params, new CallBack<DepartmentDean>() {
            @Override
            public void onSuccess(DepartmentDean result) {
                depAdapter.addAll(result.getOrganize());
                if (result.getUser().size() > 0){
                    userAdpter.addAll(result.getUser());
                }else {
                   // tvLian.setVisibility(View.GONE);
                    tvPeople.setVisibility(View.GONE);
                }
            }
        });

        depAdapter.setOnItemClickListener((viewHolder, view, i) -> startActivity(
                 new Intent(MiContactsActivity.this, DepartmentActivity2.class)
                .putExtra("OrganizeId", depAdapter.getItem(i).getOrganizeId())
                .putExtra("name", depAdapter.getItem(i).getOrganizeName())
                .putExtra("isTopChat",true)));

        userAdpter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                String userId = SPUtil.getString(Constants.USERID);
                if (userId.equals(userAdpter.getItem(i).getUserId())){
                    UItils.showToastSafe("自己不能和自己聊天");
                    return;
                }
                ChatInfo chatInfo = new ChatInfo();
                chatInfo.setType(TIMConversationType.C2C);
                chatInfo.setId(userAdpter.getItem(i).getUserId());
                //发起密聊
                if (flag){
                    chatInfo.setTopChat(true);
                }else {
                    chatInfo.setTopChat(false);
                }

                chatInfo.setChatName(userAdpter.getItem(i).getUserNick());
                Intent intent = new Intent(MiContactsActivity.this, ChatActivity.class);
                intent.putExtra(Constants.CHAT_INFO, chatInfo);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        query.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //点击搜索的时候隐藏软键盘
                // hideKeyboard(EditText);
                // 在这里写搜索的操作,一般都是网络请求数据
                search(v.getText().toString().trim());
              /*  if (!TextUtils.isEmpty(v.getText().toString().trim())) {
                }*/
                return true;
            }
            return false;
        });
    }

    private void search(String trim) {
        Map<String, Object> params = new HashMap<>();
        params.put("UserCon", trim);
        HttpClient.get(this, Constants.GETDEPARTMENTLIST, params, new CallBack<DepartmentDean>() {
            @Override
            public void onSuccess(DepartmentDean result) {
                depAdapter.replaceAll(result.getOrganize());
                userAdpter.replaceAll(result.getUser());
                if (result.getUser().size() == 0){
                    tvPeople.setVisibility(View.GONE);
                    UItils.showToastSafe("暂无相关信息");
                }
            }
        });
    }


    @OnClick({R.id.ll_friend, R.id.ll_group, R.id.ll_chuang})
    public void onViewClicked(View view) {
        //AddGroupActivity
        switch (view.getId()) {
            case R.id.ll_friend:
                startActivity(new Intent(this,AddGroupActivity.class).putExtra("type",1));
                //   UItils.startActivity(AddGroupActivity.class);
                break;
            case R.id.ll_group:
                UItils.startActivity(GroupActivity.class);
                break;
            case R.id.ll_chuang:
                startActivity(new Intent(this,ChuangFriendsActivity.class).putExtra("type",1));
               // UItils.startActivity(ChuangFriendsActivity.class);
                break;
            default:
        }
    }
}
